﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.ComponentModel;

namespace CentroVentas.Forms.Proveedor
{
    /// <summary>
    /// Lógica de interacción para FrmProveedorListar.xaml
    /// </summary>
    public partial class FrmProveedorListar : Page
    {
        public FrmProveedorListar(string Buscador)
        {
            InitializeComponent();

            if (Buscador != string.Empty)
            {
                txtBuscar.Text = Buscador;
            }

        }

        private void txtBuscar_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtBuscar.Text))
            {
                this.Grilla.DataContext = CVCore32.Class.Funciones.Proveedores.Proveedor.getProveedoresList(true);
            }
            else
            {
                this.Grilla.DataContext = CVCore32.Class.Funciones.Proveedores.Proveedor.getProveedoresList(txtBuscar.Text, true);
            }

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.Grilla.DataContext = CVCore32.Class.Funciones.Proveedores.Proveedor.getProveedoresList(true);
        }



        private void MenuModificarDatos_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ActivarProveedor_Click(object sender, RoutedEventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            //this is where the long running process should go
            worker.DoWork += (o, ea) =>
            {
                //no direct interaction with the UI is allowed from this method
                for (int i = 0; i < 100; i++)
                {
                    System.Threading.Thread.Sleep(25);
                }
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
            String asas = Grilla.Items.GetItemAt(Grilla.SelectedIndex).ToString();
            asas = asas.Replace(", ", "\",\"");
            asas = asas.Replace("{ ", "{\"");
            asas = asas.Replace(" = ", "\":\"");
            asas = asas.Replace(" }", "\"}");

            List<string> lista = CVCore32.Class.Funciones.Proveedores.Proveedor.LeerProveedorJson(asas);

            CVCore32.Class.Funciones.Proveedores.Proveedor.CambiarEstado(Convert.ToInt32(lista[0]), CVCore32.Class.Funciones.Utiles.Estado.Activo);

            this.Grilla.DataContext = CVCore32.Class.Funciones.Proveedores.Proveedor.getProveedoresList(txtBuscar.Text, !Convert.ToBoolean(chkVisible.IsChecked));
            };
            //set the IsBusy before you start the thread
            Busy.IsBusy = true;
            worker.RunWorkerAsync();
        }


        private void DesactivarProveedor_Click(object sender, RoutedEventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            //this is where the long running process should go
            worker.DoWork += (o, ea) =>
            {
                //no direct interaction with the UI is allowed from this method
                for (int i = 0; i < 100; i++)
                {
                    System.Threading.Thread.Sleep(25);
                }
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
                //work has completed. you can now interact with the UI
                String asas = Grilla.Items.GetItemAt(Grilla.SelectedIndex).ToString();
                asas = asas.Replace(", ", "\",\"");
                asas = asas.Replace("{ ", "{\"");
                asas = asas.Replace(" = ", "\":\"");
                asas = asas.Replace(" }", "\"}");

                List<string> lista = CVCore32.Class.Funciones.Proveedores.Proveedor.LeerProveedorJson(asas);

                CVCore32.Class.Funciones.Proveedores.Proveedor.CambiarEstado(Convert.ToInt32(lista[0]), CVCore32.Class.Funciones.Utiles.Estado.Inactivo);

                this.Grilla.DataContext = CVCore32.Class.Funciones.Proveedores.Proveedor.getProveedoresList(txtBuscar.Text, !Convert.ToBoolean(chkVisible.IsChecked));
                Busy.IsBusy = false;
            };
            //set the IsBusy before you start the thread
            Busy.IsBusy = true;
            worker.RunWorkerAsync();

        }

        private void chkVisible_Click(object sender, RoutedEventArgs e)
        {
            this.Grilla.DataContext = CVCore32.Class.Funciones.Proveedores.Proveedor.getProveedoresList(txtBuscar.Text, !Convert.ToBoolean(chkVisible.IsChecked));
        }















    }
}
